/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Threads;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
 * Thread que genera un ZIP con el contenido de una carpeta especifica y 
 * luego envia dicho archivo por mail a una direccion de correo especifica.
 * @author felipe
 */
public class ThreadZipMail implements Runnable {

    @Override
    public void run() {
        zip();
        mail();
    }

    /**
     * Crea un fichero ZIP.
     */
    private void zip() {
        try {
            File inFolder = new File("/home/felipe/NetBeansProjects/YahooFinances/PDF");
            File outFolder = new File("/home/felipe/NetBeansProjects/YahooFinances/PDF.zip");
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFolder)));
            BufferedInputStream in = null;
            byte[] data = new byte[1000];
            String files[] = inFolder.list();
            for (int i = 0; i < files.length; i++) {
                in = new BufferedInputStream(new FileInputStream(inFolder.getPath() + "/" + files[i]), 1000);
                out.putNextEntry(new ZipEntry(files[i]));
                int count;
                while ((count = in.read(data, 0, 1000)) != -1) {
                    out.write(data, 0, count);
                }
                out.closeEntry();
            }
            out.flush();
            out.close();
        } catch (Exception ex) {
            Logger.getLogger(ThreadZipMail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Envia un mail con el fichero ZIP generado por el metodo anterior.
     */
    private void mail() {
        try {
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.setProperty("mail.smtp.starttls.enable", "true");
            props.setProperty("mail.smtp.port", "587");
            props.setProperty("mail.smtp.user", "feliperachid@gmail.com");
            props.setProperty("mail.smtp.auth", "true");

            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(true);

            BodyPart texto = new MimeBodyPart();
            texto.setText("Graficos.zip");

            BodyPart adjunto = new MimeBodyPart();
            adjunto.setDataHandler(new DataHandler(new FileDataSource("/home/felipe/NetBeansProjects/YahooFinances/PDF.zip")));
            adjunto.setFileName("PDF.zip");

            MimeMultipart multiParte = new MimeMultipart();

            multiParte.addBodyPart(texto);
            multiParte.addBodyPart(adjunto);

            MimeMessage message = new MimeMessage(session);

            message.setFrom(new InternetAddress("feliperachid@gmail.com"));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress("feliperachid@gmail.com"));
            message.setSubject("PDFs");
            message.setContent(multiParte);

            Transport t = session.getTransport("smtp");
            t.connect("feliperachid@gmail.com", "droc472esae408");
            t.sendMessage(message, message.getAllRecipients());
            t.close();
        } catch (MessagingException ex) {
            Logger.getLogger(ThreadZipMail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
